package com.google.code.rgbgoesbetween.services;

import com.google.code.rgbgoesbetween.data.*;
import com.google.code.rgbgoesbetween.data.sqlite.SQLiteDatastore;

/**
 * <<Singleton>>
 * 
 */
public class DataProvider {
	
	private static final int PROVIDE_ID = -10;
	// Singleton "stuff"
	private static DataProvider dataProvider;
	private DataProvider() {}
	private boolean initialized = false;
	
	public static DataProvider getDataProvider() {
		if (dataProvider == null) {
			dataProvider = new DataProvider();
		}
		return dataProvider;
	}
	
	// DataProvider "stuff"
	private Datastore datastore;
	private SearchEngine searchEngine;
	
	// Now, on with the methods.
	public int createItem(Item item) {return 0;}

	public Profile createProfile(String username, String passHash) {
		return this.createProfile(username,passHash, null, null, null); // Only data we have are username and pas
	}
	

	public Profile createProfile(String username, String passHash, 
			String address, String phone, String email) {
		return datastore.createProfile(new Profile(PROVIDE_ID, username, passHash, AccountStatus.STANDARD_MEMBER,
				address, phone, email));
		}
	
	public int deleteItem(Item item) {
		return 0;}
	
	public Profile getProfile(String username, String passHash) {
		return datastore.getAccountInfo(username, passHash);
	}

	
	public boolean isSearchInitialized() {
		return this.datastore != null;
	}
	
	public Item requestItem(int id) {
		return null;}
	public Item[] requestItemByIDs(int[] ids) {
		return null;}
	public int updateProfile(Profile profile) {
		return 0;}

	/**
	 * Guarantees that dependency is properly injected. Not thread safe though! The guarantee is loose.
	 */
	public void init() {
		if(!initialized) {
			initialized = true;
			datastore = new InMemoryDatastore();
		}
	}
	
	/**
	 * Shifts to in memory data base. USE ONLY FOR TESTING !!!
	 */
	public void testInit() {
		datastore = new InMemoryDatastore();
	}
	
}
